package com.iye.puretennis.activity;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
import com.iye.puretennis.R;
import com.iye.puretennis.helper.Config;

import java.io.File;

/**
 * Created with IntelliJ IDEA.
 * User: jing wu
 * Date: 2014/11/18
 * Time: 11:28
 */
public class UploadImagePopupWindow extends PopupWindow {
    private View mainView;
    private Button btnTakePhoto, btnPickPhoto, btnCancel;

    public UploadImagePopupWindow(final Activity context, View.OnClickListener itemClick) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        mainView = inflater.inflate(R.layout.uploadimage, null);
        this.setWidth(ActionBar.LayoutParams.MATCH_PARENT);
        this.setHeight(ActionBar.LayoutParams.WRAP_CONTENT);
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        btnCancel = (Button) mainView.findViewById(R.id.btn_cancel);
        btnCancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //销毁弹出框

                dismiss();
            }
        });
        btnTakePhoto = (Button) mainView.findViewById(R.id.btn_take_photo);
        btnPickPhoto = (Button) mainView.findViewById(R.id.btn_pick_photo);
        btnTakePhoto.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //销毁弹出框

                dismiss();
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                String filePath = Environment.getExternalStorageDirectory() + "/" + System.currentTimeMillis() + ".png";
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(filePath)));
                context.getParent().startActivityForResult(intent, Config.PHOTOHRAPH);
            }
        });
        btnPickPhoto.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //销毁弹出框

                dismiss();
                Intent intent = new Intent(Intent.ACTION_PICK, null);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Config.IMAGE_UNSPECIFIED);
                context.getParent().startActivityForResult(intent, Config.PHOTOZOOM);
            }
        });
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setContentView(mainView);
        this.setFocusable(true);
    }

}
